﻿using System;
using System.Diagnostics;

using Gonte;
using Gonte.Logging;
using Gonte.Xml.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using MyUtilitiesTest.Logging;

namespace MyUtilitiesTest
{
    /// <summary>
    ///This is a test class for LoggingConfigurationTest and is intended
    ///to contain all LoggingConfigurationTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LoggingConfigurationTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void LoggingConfigurationSerializationTest()
        {
            LoggingConfiguration config = new LoggingConfiguration();

            Logger logger1 = Logger.GetLogger("Logger1");
            logger1.Level = Logger.LogLevel.All;

            config.Loggers.Add(logger1.Name, logger1);

            config.Listeners.Add("TestListener", new InMemoryTraceListener());

            XmlObjectSerializer serializer = XmlObjectSerializer.GetSerializer(config);
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<Logging>
  <Tracers />
  <Loggers>
    <Logger Name=""Logger1"" LogLevel=""All"">
      <Tracers />
    </Logger>
  </Loggers>
</Logging>";

            Assert.AreEqual(xml, serializer.SerializeToString());
        }

        [TestMethod()]
        public void LoggingConfigurationDeserializationTest()
        {
            LoggingConfiguration config = new LoggingConfiguration();

            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<Logging>
  <Loggers>
    <Logger Name=""Logger1"" LogLevel=""All"">
      <Tracers>
        <Tracer>TestTracer</Tracer>
      </Tracers>
    </Logger>
  </Loggers>
  <Tracers>
        <Tracer Name=""TestTracer"" Type=""MyUtilitiesTest.Logging.InMemoryTraceListener, MyUtilitiesTest""/>
  </Tracers>
</Logging>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(config);
            deserializer.DeserializeFromString(xml);

            Logger logger1 = config.Loggers["Logger1"];

            Assert.AreEqual("Logger1", logger1.Name);
            Assert.AreEqual(Logger.LogLevel.All, logger1.Level);

            ProxyObject listener = config.Tracers["TestTracer"];
            Assert.AreEqual("TestTracer", listener.Name);
            Assert.AreEqual("MyUtilitiesTest.Logging.InMemoryTraceListener, MyUtilitiesTest", listener.Type);

            TraceListener tracer = config.Listeners["TestTracer"];
            Assert.AreEqual(typeof(InMemoryTraceListener), tracer.GetType());

            String listenerName = logger1.Tracers[0];
            Assert.AreEqual("TestTracer", listenerName);

            TraceListener loggerTracer = logger1.Listeners[0];
            Assert.IsNotNull(loggerTracer); 
        }
    }
}
